package com.clms.system.myconfig;

import com.clms.system.myrealm.MyRealm;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.server.session.DefaultWebSessionManager;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @program: clms-third
 * @description: shiroconfig
 * @author: 大鱼
 * @create: 2021-07-17 14:25
 **/
@Configuration
public class ShiroConfig {
@Bean
    public DefaultWebSecurityManager webSecurityManager() {
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
    securityManager.setRealm(myRealm());
    return securityManager;
}
@Bean
    public MyRealm myRealm() {
    MyRealm myRealm = new MyRealm();
    myRealm.setCredentialsMatcher(credentialsMatcher());
    return myRealm;
}
@Bean
    public CredentialsMatcher credentialsMatcher() {
    HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
    hashedCredentialsMatcher.setHashAlgorithmName("MD5");
    hashedCredentialsMatcher.setHashIterations(1024);
    return hashedCredentialsMatcher;
}
@Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(){
    ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
    shiroFilterFactoryBean.setSecurityManager(webSecurityManager());
    Map<String, String> map = new LinkedHashMap<>();
    map.put("/system/login", "anon");
    map.put("/**","anon");
    shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
    return shiroFilterFactoryBean;
}
@Bean
    public FilterRegistrationBean<Filter> filterFilterRegistrationBean() {
    FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
    registrationBean.setFilter(new DelegatingFilterProxy());
    registrationBean.setName("shiroFilter");
    registrationBean.addUrlPatterns("/*");
    return registrationBean;
}
}
